{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "90ded076-1959-49b5-8021-2f954c96d92f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0ecb9839-fe23-47a1-8a5f-9e5ee07bd8a9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_data = pd.read_csv(\"能源消耗预测挑战赛公开数据/train.csv\")\n",
    "test_data = pd.read_csv(\"能源消耗预测挑战赛公开数据/test.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "827e9054-cdb6-41f7-a4b3-132d22257400",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date</th>\n",
       "      <th>id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2019122700</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2019122700</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2019122700</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2019122700</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2019122700</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59875</th>\n",
       "      <td>2019123123</td>\n",
       "      <td>494</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59876</th>\n",
       "      <td>2019123123</td>\n",
       "      <td>495</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59877</th>\n",
       "      <td>2019123123</td>\n",
       "      <td>496</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59878</th>\n",
       "      <td>2019123123</td>\n",
       "      <td>497</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>59879</th>\n",
       "      <td>2019123123</td>\n",
       "      <td>498</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>59880 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             date   id\n",
       "0      2019122700    0\n",
       "1      2019122700    1\n",
       "2      2019122700    2\n",
       "3      2019122700    3\n",
       "4      2019122700    4\n",
       "...           ...  ...\n",
       "59875  2019123123  494\n",
       "59876  2019123123  495\n",
       "59877  2019123123  496\n",
       "59878  2019123123  497\n",
       "59879  2019123123  498\n",
       "\n",
       "[59880 rows x 2 columns]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7bf021cb-9954-4830-b3bc-c9627fc4011a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_data['new_date'] = pd.to_datetime(train_data['date'], format='%Y%m%d%H')\n",
    "test_data['new_date'] = pd.to_datetime(test_data['date'], format='%Y%m%d%H')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "682eda08-76c4-446d-8e35-1055a50ab8e8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "train_data['new_date_hour'] = train_data['new_date'].dt.hour\n",
    "test_data['new_date_hour'] = test_data['new_date'].dt.hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9e5a60b9-b677-44b0-8d63-87aea9213d0e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "mean_target = train_data.groupby(['id', \"new_date_hour\"])['target'].mean().reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "a2330daa-8885-4756-b905-a404d975f5e0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "pd.merge(test_data, mean_target, \n",
    "         on=['id', \"new_date_hour\"], how='left'\n",
    ")[['date', 'id', 'target']].to_csv('submit.csv', index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1de2bb6e-fc05-469c-ba33-b8185778e393",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3.10"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
